package com.att.device;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.android.ddmlib.AdbCommandRejectedException;
import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.IDevice.DeviceState;
import com.android.ddmlib.IShellOutputReceiver;
import com.android.ddmlib.ShellCommandUnresponsiveException;
import com.android.ddmlib.TimeoutException;

public class DeviceMonitor {
//	private AndroidDebugBridge adb;
	private String osversion;
	private static Map<String, DeviceInfo> cache = new HashMap<String, DeviceInfo>();
	
	public DeviceMonitor() {

	}
	
	public List<DeviceInfo> getDevices(AndroidDebugBridge adb) {
		List<DeviceInfo> list = new ArrayList<DeviceInfo>();
		IDevice[] devices = adb.getDevices();
		for (IDevice device : devices) {
//			switch (device.getState()) {
//			case BOOTLOADER:
//				System.out.println("BOOTLOADER");
//				break;
//			case ONLINE:
//				System.out.println("ONLINE");
//			break;
//			case OFFLINE:
//				System.out.println("OFFLINE");
//				break;
//			}
			if (device.getState() == DeviceState.OFFLINE) {
				continue;
			}
			if (cache.containsKey(device.getSerialNumber())) {
				System.out.println("read cache");
				list.add(cache.get(device.getSerialNumber()));
				continue;
			}
			final DeviceInfo info = new DeviceInfo();
			info.setSerialNo(device.getSerialNumber());
			try {
				device.executeShellCommand("getprop", new IShellOutputReceiver() {
					
					@Override
					public boolean isCancelled() {
						return false;
					}
					
					@Override
					public void flush() {
						
					}
					
					private String getModel(String str) {
						String s = str;
						String split = "\r\n";
						int start = s.indexOf("[ro.product.model]:");
						if (start == -1) {
							return "";
						}
						s = s.substring(start);
						int end = s.indexOf(split);
						if (end == -1) {
							return "";
						}
						s = s.substring(0, end);
						info.setModel(s.split(":")[1]);
						return s.split(":")[1];
					}
					
					private String getBrand(String str) {
						String s = str;
						String split = "\r\n";
						int start = s.indexOf("[ro.product.brand]:");
						if (start == -1) {
							return "";
						}
						s = s.substring(start);
						int end = s.indexOf(split);
						if (end == -1) {
							return "";
						}
						s = s.substring(0, end);
						info.setBrand(s.split(":")[1]);
						return s.split(":")[1];
					}
					
					private String getOSVersion(String str) {
						String s = str;
						String split = "\r\n";
						int start = s.indexOf("[ro.build.version.release]: ");
						if (start == -1) {
							return "";
						}
						s = s.substring(start);
						int end = s.indexOf(split);
						if (end == -1) {
							return "";
						}
						s = s.substring(0, end);
						String result  = s.split(":")[1];
						osversion = result.trim().substring(1, 2);
						info.setOSVersion(result);
						return result;
					}
					
					@Override
					public void addOutput(byte[] b, int offset, int length) {
//						String split = System.getProperty("line.separator");
						String str = new String(b, offset, length);
//						System.out.println(getBrand(str));
//						System.out.println(getModel(str));
//						System.out.println(getOSVersion(str));
						getBrand(str);
						getModel(str);
						getOSVersion(str);
					}
				});
				device.executeShellCommand("dumpsys window", new IShellOutputReceiver() {
					
					@Override
					public boolean isCancelled() {
						return false;
					}
					
					@Override
					public void flush() {
					}
					
					private String getScreent(String str) {
						String split = "\r\n";
//						System.out.println(s);
						int widthStart = str.indexOf("DisplayWidth=");
						int heigthStart = str.indexOf("DisplayHeight=");
						if (widthStart == -1 || heigthStart == -1) {
//							System.out.println("+++");
							return "";
						}
						int start = Math.min(widthStart, heigthStart);
						String s = str.substring(start);
						int end = s.indexOf(split);
						String result = "";
						if (end != -1) {
							result = s.substring(0, end);
						} else {
							result = s;
						}
						String[] t = result.split(" ");
						String screenWidth = "";
						String screenHeight = "";
						if (t[0].contains("Width")) {
							screenWidth = t[0].split("=")[1];
						}
						for (String ss : t) {
							if (ss.contains("Width")) {
								screenWidth = ss.split("=")[1];
								continue;
							}
							if (ss.contains("Height")) {
								screenHeight = ss.split("=")[1];
								continue;
							}
						}
						info.setScreen(screenWidth + "x" + screenHeight);
						return screenWidth + "x" + screenHeight;
					}
					
					private String getScreent4(String str) {
						int start = str.indexOf("init=");
						if (start == -1) {
							return "";
						}
						String s = str.substring(start);
						int end = s.indexOf(" ");
						String result = s.substring(0, end);
						info.setScreen(result.split("=")[1]);
						return result.split("=")[1];
					}
					
					@Override
					public void addOutput(byte[] b, int offset, int length) {
						String str = new String(b, offset, length);
						switch(Integer.parseInt(osversion)) {
						case 1:
						case 2:
//							System.out.println(getScreent(str));
							getScreent(str);
							break;
						case 3:
							break;
						case 4:
//							System.out.println(getScreent4(str));
							getScreent4(str);
							break;
						}
						
					}
				});
			} catch (IOException e) {
				e.printStackTrace();
			} catch (TimeoutException e) {
				e.printStackTrace();
			} catch (AdbCommandRejectedException e) {
				e.printStackTrace();
			} catch (ShellCommandUnresponsiveException e) {
				e.printStackTrace();
			}
			if (info.getBrand() == null && info.getModel() == null && info.getScreen() == null) {
				continue;
			}
			list.add(info);
			cache.put(device.getSerialNumber(), info);
		}
		
		return list;
	}
	
}
